<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CommonController extends Controller
{
  //
  public function uploadImage( Request $request )
  {
    $toPath = public_path().'/sys_images';
    if ($request->hasFile('file')) {
      $file = $request->file('file');
      $fileName = \Ramsey\Uuid\Uuid::uuid1()->toString();
      $imgName = $fileName.'.'.$file->getClientOriginalExtension();
      $file->move( $toPath , $imgName );
      $image = \Image::make( $toPath.'/'.$imgName);
      if ($image->width() > 1500) {
        $image->resize(1500, null, function ($constraint) {
          $constraint->aspectRatio();
        });
        $image->save();
      }
      // 将图片传到图片服务器，并将本机的图片重命名
      if ( $fileName ) {
        $image->save( $toPath.'/'.$imgName);
      }
      return $imgName;
    } else {
      return '0';
    }
  }
}
